#include <event2/event.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
using namespace std;
void read_file_cb(evutil_socket_t fd, short event, void *arg)
{
    char buf[1024];
    int len = read(fd, buf, sizeof(buf)-1);
    if(len>0){
        printf("%s\r\n",buf);
    } else {
        printf("wait.\r\n");
        sleep(2);
    }
}

int main() {
    /* ignore pipe signal */
    if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    {
        return 1;
    }

    event_config *conf = event_config_new();
    event_config_require_features(conf, EV_FEATURE_FDS);
    event_base *base = event_base_new_with_config(conf);
    event_config_free(conf);

    int fd = open("/var/log/auth.log", O_RDONLY|O_NONBLOCK, 0);

    lseek(fd, 0, SEEK_END);

    event *fev = event_new(base, fd, EV_READ|EV_PERSIST, read_file_cb, 0);
    event_add(fev, NULL);

    event_base_dispatch(base);
    event_base_free(base);

    return 1;

}